package com.koron.ticket.work.logicticket.ychotsafework;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.FastJsonUtils;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.work.common.bean.TicketBaseMsg;
import com.koron.ticket.work.common.service.FifteenTablesService;
import com.koron.ticket.work.common.service.YcCommonService;
import com.koron.ticket.work.enums.TicketNodeStatus;
import com.koron.ticket.work.enums.TicketStatus;
import com.koron.ticket.work.enums.TicketType;
import com.koron.ticket.work.enums.WorkFlowType;
import com.koron.ticket.work.logicticket.hightask.bean.HighTaskDto;
import com.koron.ticket.work.logicticket.ychotsafework.bean.HeatAnalysisBean;
import com.koron.ticket.work.logicticket.ychotsafework.bean.HotWorkSafeDto;
import com.koron.ticket.work.logicticket.ychotsafework.bean.YcHotWorkSafeTicketPrint;
import com.koron.ticket.work.logicticket.ychotsafework.mapper.HeatAnalysisBeanMapper;
import com.koron.ticket.work.securityarrangement.bean.SecurityArrangement;
import com.koron.ticket.work.workPermit.bean.WorkPermit;
import com.koron.ticket.work.workPermit.service.WorkPermitService;
import com.koron.ticket.work.workend.bean.WorkEnd;
import com.koron.ticket.work.workflow.bean.WorkFlow;
import com.koron.ticket.work.workflow.service.WorkFlowService;
import com.koron.ticket.work.worklocation.bean.WorkLocationBean;
import com.koron.ticket.work.workmember.bean.WorkMember;
import com.koron.ticket.work.workteam.bean.WorkTeam;
import com.koron.ticket.work.workticket.bean.WorkTicket;
import com.koron.ticket.work.workticket.service.WorkTicketService;
import com.koron.ticket.work.workycinfo.bean.TicketYcInfoBean;
import com.koron.ticket.work.workycinfo.service.WorkYcInfoService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class HotWorkSafeService {

    @Autowired
    private FifteenTablesService fifteenTablesService;

    @Autowired
    private WorkFlowService workFlowService;

    @Autowired
    private WorkTicketService workTicketService;

    @Autowired
    private WorkPermitService workPermitService;

    @Autowired
    private WorkYcInfoService workycinfoservice;

    @Autowired
    private YcCommonService yccommonservice;

    @TaskAnnotation("writeSave")
    public Response writeSave(SessionFactory factory, HotWorkSafeDto dto) {
        workTicketService.checkTimeOut(factory, dto.getId(), dto.getUpdateTime());//检查信息是否过期
        String id = CodeTools.getCode32();

        // 工作票主表
        WorkTicket workticket = Convert.convert(WorkTicket.class, dto);
        // 动火安全作业票
        TicketYcInfoBean ticketycinfobean = Convert.convert(TicketYcInfoBean.class, dto);
        // 工作票基础信息
        TicketBaseMsg ticketbasemsg = Convert.convert(TicketBaseMsg.class, dto);

        ticketycinfobean.setTicketType(workticket.getType());
        if (StrUtil.isNotBlank(workticket.getId())) {
            workTicketService.update(factory, workticket);
            ticketycinfobean.setWorkTicketId(workticket.getId());
        } else {
            workticket.setId(id);
            ticketycinfobean.setWorkTicketId(id);
            workTicketService.insert(factory, workticket);
        }
        workycinfoservice.save(factory, ticketycinfobean);

        // 动火分析
        HeatAnalysisBeanMapper heatanalysisbeanmapper = factory.getMapper(HeatAnalysisBeanMapper.class);
        heatanalysisbeanmapper.deleteByWorkTicketId(dto.getId());
        if(CollUtil.isNotEmpty(dto.getHeatanAlysislist())) {
            dto.getHeatanAlysislist().stream().forEach(g->{
                g.setId(CodeTools.getCode32());
                g.setWorkTicketId(workticket.getId());
            });
            heatanalysisbeanmapper.batchInsert(dto.getHeatanAlysislist());
        }

        // 更新工作票基础信息
        yccommonservice.updateTicketMsg(factory, workticket.getId(), ticketbasemsg);

        return Response.success(workticket.getId());
    }


    @TaskAnnotation("writeSubmit")
    public Response writeSubmit(SessionFactory factory, HighTaskDto dto) {
        WorkTicket workticket = Convert.convert(WorkTicket.class, dto);
        String workTicketId = yccommonservice.ycWriteSubmitByWorkFlowType(factory, workticket, WorkFlowType.HighTaskTicketWork.getWorkFlow());
        return Response.success(workTicketId);
    }

    @TaskAnnotation("signSave")
    public Response signSave(SessionFactory factory, WorkFlow workFlow) {
//        workTicketService.checkTimeOut(factory, workFlow.getWorkTicketId(), workFlow.getUpdateTime());//检查信息是否过期
        yccommonservice.signSave(factory, workFlow);
//        fifteenTablesService.signOrCountersignOrAcceptSave(factory, workFlow);
        return Response.success("提交/保存成功");
    }

    @TaskAnnotation("countersignSave")
    public Response countersignSave(SessionFactory factory, WorkFlow workFlow) {
//        workTicketService.checkTimeOut(factory, workFlow.getWorkTicketId(), workFlow.getUpdateTime());//检查信息是否过期
        yccommonservice.countersignSave(factory, workFlow);
//        fifteenTablesService.signOrCountersignOrAcceptSave(factory, workFlow);
        return Response.success("提交/保存成功");
    }

    @TaskAnnotation("acceptSave")
    public Response acceptSave(SessionFactory factory, WorkFlow workFlow) {
//        workTicketService.checkTimeOut(factory, workFlow.getWorkTicketId(), workFlow.getUpdateTime());//检查信息是否过期
        yccommonservice.acceptSave(factory, workFlow);
//        fifteenTablesService.signOrCountersignOrAcceptSave(factory, workFlow);
        return Response.success("提交/保存成功");
    }

    @TaskAnnotation("permitSave")
    public Response permitSave(SessionFactory factory, WorkPermit workPermit) {
        yccommonservice.permitSave(factory, workPermit);
        return Response.success("待许可-提交/保存成功");
    }

    @TaskAnnotation("ticketEndSave")
    public Response ticketEndSubmitOrSave(SessionFactory factory, WorkEnd workEnd) {
        List<WorkFlow> workFlowList = workEnd.getWorkFlowList();
        workFlowService.deleteByWorkTicketId(factory, workEnd.getWorkTicketId(), TicketStatus.WORK_END.getStatus());
        workFlowService.batchInsert(factory, workFlowList);
        return Response.success("工作票待终结-提交/保存成功");
    }

    @TaskAnnotation("workflowUpdate")
    public Response workflowUpdate(SessionFactory factory, RedirectQuery redirectQuery) throws ParseException {
        Integer operation = redirectQuery.getOperation();
        String workTicketId = redirectQuery.getDataId();
        WorkTicket workTicket = workTicketService.queryById(factory, workTicketId);
        // 提交前状态
        Integer status = workTicket.getStatus();
        String type = workTicket.getType();

        // 1. 如果被驳回，返回对应状态，仅删除签名的数据，其他的数据不做处理
        if (operation == 1) {
            String rejectNodeCode = redirectQuery.getRejectNodeCode();
            rejectNodeCode = rejectNodeCode.replace("call-", "");
            if (rejectNodeCode.equals(TicketNodeStatus.WRITE.getNodeName())) {
                Integer rejectStatus = TicketStatus.WRITE.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.SIGN.getNodeName())) {
                Integer rejectStatus = TicketStatus.SIGN.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.COUNTERSIGN.getNodeName())) {
                Integer rejectStatus = TicketStatus.COUNTERSIGN.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.ACCEPT.getNodeName())) {
                Integer rejectStatus = TicketStatus.ACCEPT.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (StrUtil.contains(rejectNodeCode, TicketNodeStatus.PERMIT.getNodeName())) {
                // 高处作业有两个许可，平台返回的code 包含PERMIT都认为是许可节点
                Integer rejectStatus = TicketStatus.PERMIT.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                // 20211111: 如何是待许可状态，讲预览标志设置成未预览
                workTicket.setPreviewFlag(1);
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.WORK_END.getNodeName())) {
                Integer rejectStatus = TicketStatus.WORK_END.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workTicket.setStatus(rejectStatus);
            }
            workTicketService.update(factory, workTicket);
        } // 2. 如果通过，工作票进入下一个状态，并根据状态的不同进行不同的操作
        else if (operation == 32) {
            // 下一个节点编码
            String curNodeCode = redirectQuery.getCurNodeCode();
            curNodeCode = curNodeCode.replace("call-", "");
            String formVariables = redirectQuery.getFormVariables();
            if (status.equals(TicketStatus.WRITE.getStatus())) {
                workTicket.setStatus(2);
            }
            // 2.1 如果是签发状态，保存签名信息
            if (status.equals(TicketStatus.SIGN.getStatus())) {
                fifteenTablesService.saveWorkFLow(factory, formVariables);
                // 生成工作票号
                if (StrUtil.isBlank(workTicket.getCode())) {
                    String code = fifteenTablesService.getLastCodeByUnitAndTicketTypeNew(factory, workTicket.getUnit(), type);
                    if (StringUtils.isEmpty(code)) {
                        return Response.fail("工作票票号生成有问题，请联系运维人员");
                    }
                    workTicket.setCode(code);
                }
                workTicket.setStatus(3);//放弃自增控制，改为手动控制
            }
            else if (status.equals(TicketStatus.COUNTERSIGN.getStatus())) {
                // 保存签名信息
                fifteenTablesService.saveWorkFLow(factory, formVariables);
                workTicket.setStatus(4);//放弃自增控制，改为手动控制
            }
            // 2.3 如果是待接收状态：1.保存签名信息；2.将预览标志设为未预览
            else if (status.equals(TicketStatus.ACCEPT.getStatus())) {
                // 保存签名信息
                fifteenTablesService.saveWorkFLow(factory, formVariables);
                // 将预览标志设为未预览
                workTicket.setPreviewFlag(1);
                workTicket.setStatus(5);//放弃自增控制，改为手动控制
            }
            // 2.4 如果是待许可状态，保存签名信息，以及待许可信息
            else if (status.equals(TicketStatus.PERMIT.getStatus())) {
                // 保存许可信息
                WorkPermit workPermit = JSONObject.parseObject(formVariables, WorkPermit.class);
                // 保存签名信息
                List<WorkFlow> workFlowList = workPermit.getWorkFlowList();
                if (CollUtil.isNotEmpty(workFlowList)) {
                    for (WorkFlow workFLow : workFlowList) {
                        workFlowService.save(factory, workFLow);
                    }
                }
                workTicket.setStatus(6);//放弃自增控制，改为手动控制
            }
            else if (status.equals(TicketStatus.WORK_END.getStatus())) {
                WorkEnd workend = FastJsonUtils.getJsonToBean(formVariables, WorkEnd.class);
                List<WorkFlow> workFlowList = workend.getWorkFlowList();
                for (WorkFlow workFlow : workFlowList) {
                    workFlowService.save(factory, workFlow);
                }
                workTicket.setStatus(7);//放弃自增控制，改为手动控制
            }
            workTicketService.update(factory, workTicket);
        } else if (operation == 256) {
            Integer rejectStatus = TicketStatus.WRITE.getStatus();
            fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
            workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
            workTicket.setStatus(rejectStatus);
            workTicketService.update(factory, workTicket);
        } else {
            return Response.fail("操作类型为：" + operation + ",请联系运维人员");
        }

        return Response.success("高处作业票提交成功");
    }

    @TaskAnnotation("workflowUpdateEnd")
    public Response workflowUpdateEnd(SessionFactory factory, RedirectQuery redirectQuery) throws ParseException {
        String formVariables = redirectQuery.getFormVariables();
        WorkEnd workend = FastJsonUtils.getJsonToBean(formVariables, WorkEnd.class);
        List<WorkFlow> workFlowList = workend.getWorkFlowList();
        for (WorkFlow workFlow : workFlowList) {
            workFlowService.save(factory, workFlow);
        }
//        fifteenTablesService.saveWorkFLow(factory, formVariables);
        WorkTicket workticket = new WorkTicket();
        workticket.setId(workend.getWorkTicketId());
        workticket.setStatus(8);
        workTicketService.update(factory, workticket);
        return Response.success("工作票已归档", workend.getId());
    }

    @TaskAnnotation("workflowUpdateCancel")
    public Response workflowUpdateCancel(SessionFactory factory, RedirectQuery redirectQuery) {
        fifteenTablesService.workflowUpdateCancel(factory, redirectQuery);
        return Response.success("工作票已作废");
    }

    public void deleteById(SessionFactory factory, String id) {
        fifteenTablesService.deleteById(factory, id);
    }

    /**
     * 详情
     * @param factory
     * @param id
     * @return
     */
    public HotWorkSafeDto getById(SessionFactory factory, String id) {
        // 主表
        WorkTicket workTicket = workTicketService.queryById(factory, id);
        // 工作票基础信息
        TicketBaseMsg ticketBaseMsg = yccommonservice.queryTicketMsg(factory, id);
        // 高处票基础信息
        TicketYcInfoBean ycInfo = workycinfoservice.getYcInfo(factory, id);
        // 签名信息
        List<WorkFlow> workFlows = workFlowService.queryByTicketId(factory, id);

        List<HeatAnalysisBean> heatAnalysisBeans = factory.getMapper(HeatAnalysisBeanMapper.class).queryByWorkTicketId(id);

        HotWorkSafeDto dto = new HotWorkSafeDto();
        BeanUtil.copyProperties(workTicket, dto);
        BeanUtil.copyProperties(ticketBaseMsg, dto);
        BeanUtil.copyProperties(ycInfo, dto);
        dto.setHeatanAlysislist(heatAnalysisBeans);
        dto.setId(workTicket.getId());
        dto.setWorkFlowList(workFlows);
        return dto;
    }

    /**
     * 高处作业票、动火安全作业票
     * 获取打印数据
     *
     * @param factory
     * @param ticketId
     * @return
     */
    @TaskAnnotation("getPrintData")
    public List<YcHotWorkSafeTicketPrint> getPrintData(SessionFactory factory, String ticketId) {
        List<YcHotWorkSafeTicketPrint> printlist = new ArrayList();

        // 作业类别字典
        Map<String, String> dataDictByCode = DataDictTool.getDataDictByCode("twoTicketManagement", "ticket_task_type");

        // 主表信息
        WorkTicket workTicket = workTicketService.queryById(factory, ticketId);
        // 信息
        TicketYcInfoBean ycInfo = workycinfoservice.getYcInfo(factory, ticketId);
        // 所有签名信息
        List<WorkFlow> workFlowlist = workFlowService.queryByTicketId(factory, ticketId);
        // 基础信息
        TicketBaseMsg ticketBaseMsg = yccommonservice.queryTicketMsg(factory, ticketId);
        String locationStr = "";
        String workDeptStr = "";
        String memberNameListString = "";
        // 工作票状态
        Integer status = workTicket.getStatus();
        // 工作地点、作业地点
        if (CollUtil.isNotEmpty(ticketBaseMsg.getWorkLocationList())) {
            locationStr = ticketBaseMsg.getWorkLocationList().stream().map(WorkLocationBean::getLocationName).collect(Collectors.joining(","));
        }
        // 工作班组、作业单位
        if (CollUtil.isNotEmpty(ticketBaseMsg.getWorkTeamList())) {
            workDeptStr = ticketBaseMsg.getWorkTeamList().stream().map(WorkTeam::getWorkTeamName).collect(Collectors.joining(","));
        }
        // 工作班组成员、作业人
        if (CollUtil.isNotEmpty(ticketBaseMsg.getWorkMemberList())) {
            memberNameListString = ticketBaseMsg.getWorkMemberList().stream().map(WorkMember::getMemberName).collect(Collectors.joining(","));
        }

        Map<String, String> dictByClassCode = CodeTools.getDictByClassCode("twoTicketManagement", "ticket_hot_operation_level");


        YcHotWorkSafeTicketPrint hotworksafeprint = new YcHotWorkSafeTicketPrint();
        hotworksafeprint.setUnitName(workTicket.getUnitName());
        hotworksafeprint.setAppTime(DateUtil.format(ycInfo.getAppTime(), "yyyy年MM月dd日 HH时mm分"));
        hotworksafeprint.setCode(workTicket.getCode());
        hotworksafeprint.setLocationStr(locationStr);
        hotworksafeprint.setDirectorName(workTicket.getDirectorName());
        hotworksafeprint.setGuardianName(ycInfo.getGuardianName());
        hotworksafeprint.setOtherSpecialWork(ycInfo.getOtherSpecialWork());
        hotworksafeprint.setOtherSpecialNum(ycInfo.getOtherSpecialNum());
        hotworksafeprint.setRiskResults(ycInfo.getRiskResults());
        hotworksafeprint.setSecurityName(ycInfo.getSecurityName());
        hotworksafeprint.setAcceptName(ycInfo.getAcceptName());
        hotworksafeprint.setMemberNameListString(memberNameListString);
        hotworksafeprint.setContent(workTicket.getContent());
        hotworksafeprint.setEstimatedStartTime(DateUtil.format(workTicket.getEstimatedStartTime(), "yyyy年MM月dd日 HH时mm分"));
        hotworksafeprint.setEstimatedEndTime(DateUtil.format(workTicket.getEstimatedEndTime(), "yyyy年MM月dd日 HH时mm分"));
        hotworksafeprint.setCreateName(workTicket.getCreateUserName());

        hotworksafeprint.setHotLevel(dictByClassCode.get(ycInfo.getHotLevel()));// 动火级别
        hotworksafeprint.setHotWay(ycInfo.getHotWay());// 动火方式
        hotworksafeprint.setHotWorkFirstSign(ycInfo.getHotMeasuresName());// 动火初审人


        // 动火安全作业票
        if (workTicket.getType().equals(TicketType.HotSafetyTicket.getType())) {
            List<HeatAnalysisBean> heatAnalylist = factory.getMapper(HeatAnalysisBeanMapper.class).queryByWorkTicketId(workTicket.getId());
            if (CollUtil.isNotEmpty(heatAnalylist)) {
                for (HeatAnalysisBean heatAnaly : heatAnalylist) {
                    if ("name0".equals(heatAnaly.getType())) {
                        hotworksafeprint.setAnalysisTime1(DateUtil.format(heatAnaly.getAnalysisTime(), "yyyy年MM月dd日 HH时mm分"));
                        hotworksafeprint.setLocationName1(heatAnaly.getLocationName());
                        hotworksafeprint.setAnalysisLel1(heatAnaly.getAnalysisLel());
                        hotworksafeprint.setAnalysisName1(heatAnaly.getAnalysisName());
                    }
                    if ("name1".equals(heatAnaly.getType())) {
                        hotworksafeprint.setAnalysisTime2(DateUtil.format(heatAnaly.getAnalysisTime(), "yyyy年MM月dd日 HH时mm分"));
                        hotworksafeprint.setLocationName2(heatAnaly.getLocationName());
                        hotworksafeprint.setAnalysisLel2(heatAnaly.getAnalysisLel());
                        hotworksafeprint.setAnalysisName2(heatAnaly.getAnalysisName());
                    }
                    if ("name2".equals(heatAnaly.getType())) {
                        hotworksafeprint.setAnalysisTime3(DateUtil.format(heatAnaly.getAnalysisTime(), "yyyy年MM月dd日 HH时mm分"));
                        hotworksafeprint.setLocationName3(heatAnaly.getLocationName());
                        hotworksafeprint.setAnalysisLel3(heatAnaly.getAnalysisLel());
                        hotworksafeprint.setAnalysisName3(heatAnaly.getAnalysisName());
                    }

                }
            }
        }

        // 签发信息 -- 作业单位负责人意见
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.SIGN.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.SIGN.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setWorkUnitPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setWorkUnitTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setWorkUnitMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 会签信息 -- 动火点所在车间负责人
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.COUNTERSIGN.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.ACCEPT.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setHotWorkPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setHotWorkTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setHotWorkMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 接收信息 -- 动火前，岗位顶班班长验票
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.ACCEPT.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.ACCEPT.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setSafeSignPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setSafeSignTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setSafeSignMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 许可信息 -- 动火前，岗位顶班班长验票
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.PERMIT.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.PERMIT.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setHotWorkSpPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setHotWorkSpTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setHotWorkSpMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 工作结束信息 --动火前，岗位顶班班长验票
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.WORK_END.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.TICKET_END.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setTopShiftPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setTopShiftTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setTopShiftMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 工作票结束信息
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.TICKET_END.getStatus()) {
                Predicate<WorkFlow> predicateNode = f -> f.getStatus().equals(TicketStatus.TICKET_END.getStatus());//过滤节点
                Predicate<WorkFlow> predicateType = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Optional<WorkFlow> first = workFlowlist.stream().filter(predicateNode.and(predicateType)).findFirst();
                Date sigTime = first.map(WorkFlow::getOperationTime).orElse(null);
                hotworksafeprint.setCompletedPerson(first.map(WorkFlow::getName).orElse(""));
                hotworksafeprint.setCompletedTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));
                hotworksafeprint.setCompletedMsg(first.map(WorkFlow::getOpinion).orElse(""));
            }
        }

        // 安全措施
        for (SecurityArrangement securityArrangement : ticketBaseMsg.getSecurityArrangementList()) {
            YcHotWorkSafeTicketPrint securityPrint = new YcHotWorkSafeTicketPrint();
            BeanUtil.copyProperties(hotworksafeprint, securityPrint);
            securityPrint.setSecurityitem(securityArrangement.getItem());
            if (ObjectUtil.isNotNull(securityArrangement.getIsInvolve())) {
                Integer isInvolve = Integer.valueOf(securityArrangement.getIsInvolve());
                switch (isInvolve) {
                    case 1:
                        securityPrint.setSecurityIsInvolve("是");
                        break;
                    case 0:
                        securityPrint.setSecurityIsInvolve("否");
                        break;
                    default:
                        securityPrint.setSecurityIsInvolve("");
                        break;
                }
                securityPrint.setSecurityPerson(securityArrangement.getInvolveName());
            }
            printlist.add(securityPrint);
        }
        return printlist;
    }

}
